from selenium import webdriver


class Key:

    # 1.基本用法--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    # 初始化浏览器对象
    def __init__(self, driver):
        self.driver = webdriver.Chrome()
        # self.driver = driver
        # 静默启动谷歌浏览器
        # option = webdriver.ChromeOptions()
        # option.add_argument('headless')
        # self.driver = webdriver.Chrome(chrome_options=option)

    # 打开网址
    def get(self, url):
        self.driver.get(url)

    # 设置窗口最大化
    def set_max_win(self):
        self.driver.maximize_window()

    # 设置窗口最小化
    def set_min_win(self):
        self.driver.minimize_window()

    # 设置窗口分辨率为宽width，高height
    def set_win(self, width, height):
        self.driver.set_window_size(width, height)

    # 刷新页面
    def refresh(self):
        try:
            self.driver.refresh()
            print("刷新页面")
        except Exception as e:
            print("刷新失败")

    # 页面前进
    def forword(self):
        self.driver.forward()

    # 页面后退
    def back(self):
        self.driver.back()

    # 2.获取页面基本属性---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    # 获取网页标题
    def get_title(self):
        return self.driver.title

    # 获取当前网址
    def get_current_url(self):
        return self.driver.current_url

    # 获取浏览器名称
    def get_name(self):
        return self.driver.name

    # 获取网页源码
    def get_page_source(self):
        return self.driver.page_source

    # 3.基本定位---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    """
        ID = "id"
        XPATH = "xpath"
        LINK_TEXT = "link text"
        PARTIAL_LINK_TEXT = "partial link text"
        NAME = "name"
        TAG_NAME = "tag name"
        CLASS_NAME = "class name"
        CSS_SELECTOR = "css selector"
    """

    # 定位一个元素      (定位方法, 定位属性)
    def By(self, name, value):
        return self.driver.find_element(name, value)

    # 定位多个元素,以列表的形式     (定位方法, 定位属性)
    def Bys(self, name, value):
        return self.driver.find_elements(name, value)

    # 4.获取页面元素属性---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    # 获取某一元素的相关属性      (定位方法, 定位属性, 获取的属性名称)
    def get_attribute(self, name, value, attribute_name):
        return self.By(name, value).get_attribute(attribute_name)

    # 获取多个元素的某一属性      (定位方法, 定位属性, 获取的属性)
    def get_attributes(self, name, value, txt):
        return self.Bys(name, value).get_attribute(txt)

    # 获取一个元素的相关属性 通过点的方式调用出来     (定位方法, 定位属性, 获取的属性)
    def get_place(self, name, value, attributes):
        element = self.By(name, value)
        return getattr(element, attributes)

    # 5.页面交互操作---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    #输入文本
    def send_keys(self,name,value,txt):
        self.By(name,value).send_keys(txt)
    #点击
    def click(self,name,value):
        self.By(name,value).click()
    #清空文本
    def clear(self,name,value):
        self.By(name,value).clear()
    #回车确认
    def submit(self,name,value):
        self.By(name,value).submit()


